public class MyLinkedList {
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last; //代表双向链表的尾巴
    public int size(){
        ListNode cur = head;
        int count = 0;
        while (cur.next != null){
            cur = cur.next;
            count++;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }else {
                cur = cur.next;
            }
        }return false;
    }
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }
    public void addIndex(int index,int data){
        checkIndex(index);
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }
        ListNode cur = searchIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    private void checkIndex(int index){
        if (index < 0 || index > size()){
            throw new IndexOutOfException("index不合法"+index);
        }
    }
    private ListNode searchIndex(int index){
        ListNode cur = head;
        while (index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    public void remove(int key){//删除单独与key相同的节点
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                if (cur == head){
                    if (head != null){
                        cur = cur.next;
                        cur.prev = null;
                        head = cur;
                        return;
                    }else {
                        last = null;
                        return;
                    }
                }else if (cur == last){
                    cur = cur.prev;
                    cur.next = null;
                    last = cur;
                    return;
                }
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;
            }else {
                cur = cur.next;
            }
        }
        if (cur == null){
            System.out.println("你要删除的数不存在");
        }
    }
    public void allRemove(int key){//删除所有与key相同的节点
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                if (cur == head){
                    if (head != null){
                        cur = cur.next;
                        cur.prev = null;
                        head = cur;
                        return;
                    }else {
                        last = null;
                        return;
                    }
                }else if (cur == last){
                    cur = cur.prev;
                    cur.next = null;
                    last = cur;
                    return;
                }
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                cur = cur.next;
            }else {
                cur = cur.next;
            }
        }
        if (cur == null){
            System.out.println("你要删除的数不存在");
        }
    }
    public void clear(){
        ListNode cur = head;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
//            cur.prev = null;
//            cur = cur.next;
//            cur.prev.next = null;
        }
        head = null;
        last = null;
    }
}
